//
//  HQCSpecificTableViewCell.m
//  NursingManages
//
//  Created by Zhen Huang on 17/6/7.
//  Copyright © 2017年 huangzhen. All rights reserved.
//

#import "HQCSpecificTableViewCell.h"
#import "HNewQCSCollectionViewCell.h"
#import "SizeToFit.h"
#import "Model_Hr_Qc_Spec.h"
#import "Model_Hr_Qc_Spec_Item.h"
#import "DQFormViewController.h"
@implementation HQCSpecificTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.pointLabel];
        [self.contentView addSubview:self.pointTextfield];
        [self.contentView addSubview:self.frequencyLabel];
        [self.contentView addSubview:self.frequencyTextfield];
        [self.contentView addSubview:self.reducePointLabel];
        [self.contentView addSubview:self.chooseReasonButton];
        [self.contentView addSubview:self.reducePointTextView];
        [self.contentView addSubview:self.ExplainLabel];
        [self.contentView addSubview:self.button];
    }
    return self;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, ScreenWidth - 20, 30)];
        _titleLabel.font = [UIFont systemFontOfSize:17];
    }
    return _titleLabel;
}
- (UILabel *)pointLabel
{
    if (!_pointLabel) {
        _pointLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, self.titleLabel.bottom + 10, 50, 30)];
        _pointLabel.text = @"得分:";
        _pointLabel.font = [UIFont systemFontOfSize:17];

    }
    return _pointLabel;
}
- (UITextField *)pointTextfield
{
    if (!_pointTextfield) {
        _pointTextfield = [[UITextField alloc]initWithFrame:CGRectMake(self.pointLabel.right + 10, self.titleLabel.bottom + 10, 100, 30)];
        _pointTextfield.delegate = self;
        _pointTextfield.textAlignment = NSTextAlignmentCenter;
        _pointTextfield.borderStyle = UITextBorderStyleRoundedRect;
        _pointTextfield.layer.cornerRadius = 6.0;
        _pointTextfield.layer.masksToBounds = YES;
        
        
    }
    return _pointTextfield;
}

- (UILabel *)frequencyLabel
{
    if (!_frequencyLabel) {
        _frequencyLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.pointTextfield.right + 10,self.titleLabel.bottom + 10, 50, 30)];
        _frequencyLabel.text = @"频次:";
        _frequencyLabel.font = [UIFont systemFontOfSize:17];
    }
    return _frequencyLabel;
}

- (UITextField *)frequencyTextfield
{
    if (!_frequencyTextfield) {
        _frequencyTextfield = [[UITextField alloc]initWithFrame:CGRectMake(self.frequencyLabel.right + 10, self.titleLabel.bottom + 10, 100, 30)];
        _frequencyTextfield.delegate = self;
        _frequencyTextfield.textAlignment = NSTextAlignmentCenter;
        _frequencyTextfield.borderStyle = UITextBorderStyleRoundedRect;
        _frequencyTextfield.layer.cornerRadius = 6.0;
        _frequencyTextfield.layer.masksToBounds = YES;
        _frequencyTextfield.keyboardType = UIKeyboardTypeNumberPad;

    }
    return _frequencyTextfield;
}
- (UILabel *)reducePointLabel
{
    if (!_reducePointLabel) {
        _reducePointLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, self.pointLabel.bottom + 10, 100, 30)];
        _reducePointLabel.text = @"扣分原因:";
        _reducePointLabel.font = [UIFont systemFontOfSize:17];
    }
    return _reducePointLabel;
}


- (UITextView *)reducePointTextView
{
    if (!_reducePointTextView) {
        _reducePointTextView = [[UITextView alloc]initWithFrame:CGRectMake(10, self.reducePointLabel.bottom + 10, ScreenWidth - 120, 40)];
        _reducePointTextView.delegate = self;
        _reducePointTextView.layer.borderWidth = 0.1;
        _reducePointTextView.layer.cornerRadius = 6.0;
        _reducePointTextView.layer.masksToBounds = YES ;
    }
    return _reducePointTextView;
}

- (UIButton *)chooseReasonButton
{
    if (!_chooseReasonButton) {
        _chooseReasonButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _chooseReasonButton.backgroundColor = RGB(88, 172, 233);
        [_chooseReasonButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _chooseReasonButton.frame = CGRectMake(self.reducePointTextView.right + 10, self.reducePointLabel.bottom + 10, 95, 40);
        [_chooseReasonButton setTitle:@"选择原因" forState:UIControlStateNormal];
        _chooseReasonButton.layer.masksToBounds = YES;
        _chooseReasonButton.layer.cornerRadius = 6;
        _chooseReasonButton.titleLabel.font = [UIFont systemFontOfSize:16];
    }
    return _chooseReasonButton;
}
- (UILabel *)ExplainLabel
{
    if (!_ExplainLabel) {
        _ExplainLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, self.reducePointTextView.bottom + 10, ScreenWidth - 20, 30)];
        _ExplainLabel.textColor = [UIColor lightGrayColor];
        _ExplainLabel.font = [UIFont systemFontOfSize:14];
        _ExplainLabel.numberOfLines = 0;
    }
    return _ExplainLabel;
}

- (UIButton *)button
{
    if (!_button) {
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        _button.frame = CGRectMake(ScreenWidth - 100, self.titleLabel.bottom + 10, 90, 30);
        [_button setTitle:@"设置关键指标" forState:UIControlStateNormal];
        _button.titleLabel.font = [UIFont systemFontOfSize:14];
        _button.backgroundColor = [UIColor redColor];
    }
    return _button;
}
- (UICollectionView *)CollectionView {
//    if (!_CollectionView) {
//        _collectionViewLayout                          = [[UICollectionViewFlowLayout alloc] init]; // 自定义的布局对象
//        _CollectionView                                = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64 , ScreenWidth, ScreenHeight - 64 ) collectionViewLayout:_collectionViewLayout];
//        //移动方向
//        _collectionViewLayout.scrollDirection          = UICollectionViewScrollDirectionHorizontal;
//        //是否显示
//        _CollectionView.showsHorizontalScrollIndicator = NO;
//        //是否有反弹效果
//        _CollectionView.bounces                        = YES;
//        //是否整页翻页
//        _CollectionView.pagingEnabled                  = YES;
//        //数据源代理
//        _CollectionView.dataSource                     = self;
//        _CollectionView.delegate                       = self;
//        //滚动返回至顶部
//        _CollectionView.scrollsToTop                   = NO;
//        _CollectionView.backgroundColor                = [UIColor redColor];
//        // 注册cell
//        [_CollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
//    }
    
    
    
    
//    UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayout alloc]init];
//    
//    flowLayOut.itemSize = CGSizeMake(50, 50);
//    
//    flowLayOut.minimumInteritemSpacing = 3;
//    
//    flowLayOut.minimumLineSpacing = 5;
//    
//    flowLayOut.scrollDirection = UICollectionViewScrollDirectionVertical;
//    
//    
//    flowLayOut.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    
    
    if (!_CollectionView) {
        
        UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayout alloc]init];
        
        flowLayOut.itemSize = CGSizeMake(50, 50);
        
        flowLayOut.minimumInteritemSpacing = 3;
        
        flowLayOut.minimumLineSpacing = 5;
        
        flowLayOut.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        
        flowLayOut.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
        

        
        _CollectionView = [[UICollectionView alloc]initWithFrame:
                           CGRectMake(40, 20, ScreenWidth - 40, ScreenHeight) collectionViewLayout:flowLayOut];
        _CollectionView.backgroundColor = [UIColor whiteColor];
        
        _CollectionView.dataSource = self;
        
        _CollectionView.delegate = self;
        
        //    _CollectionView.backgroundColor = RGB(227, 227, 227);
        
//        _CollectionView.backgroundColor = [UIColor redColor];
        
        [_CollectionView registerClass:[HNewQCSCollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
        

    }
    
    
    return _CollectionView;
}

- (void)awakeFromNib {
    [super awakeFromNib];
}
- (void)setUpcell:(HQCSpecificTableViewCell *)cell
            array:(NSArray *)array
        itemArray:(NSArray *)itemArray
             flag:(NSString *)flag
            block: (void(^)(NSInteger ))callback{
    
    self.array = array;
    self.itemArray = itemArray;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.titleLabel.numberOfLines = 0;
    CGFloat height = [SizeToFit getHeightByWidth:ScreenWidth - 20 title:cell.titleLabel.text font:[UIFont systemFontOfSize:18]];
    cell.titleLabel.frame = CGRectMake(10, 10, ScreenWidth - 20, height);
    cell.pointLabel.top = cell.titleLabel.bottom + 5;
    cell.frequencyLabel.top = cell.titleLabel.bottom + 5;
    cell.frequencyTextfield.top = cell.titleLabel.bottom + 5;
    cell.pointTextfield.top = cell.titleLabel.bottom + 5;
    cell.reducePointLabel.top = cell.pointLabel.bottom + 5;
    cell.chooseReasonButton.top = cell.reducePointLabel.bottom + 5;
    cell.reducePointTextView.top = cell.reducePointLabel.bottom + 5;
    CGFloat Explainheight = [SizeToFit getHeightByWidth:ScreenWidth - 20 title:cell.ExplainLabel.text font:[UIFont systemFontOfSize:14]];
    cell.ExplainLabel.numberOfLines = 0;
    NSInteger HEIGHT = cell.titleLabel.height + self.pointLabel.height + self.reducePointLabel.height + self.reducePointTextView. height + cell.ExplainLabel.height + 55;
    
    cell.ExplainLabel.frame = CGRectMake(10, cell.reducePointTextView.bottom +10, ScreenWidth - 20, Explainheight);
    
    if ([flag isEqualToString:@"1"]) {
        
        self.button.hidden = NO;
        
    }else {
    
        self.button.hidden = YES;
    
    }
    
    callback (HEIGHT);

}



// 设置单元格的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.array.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return self.itemArray.count;
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    Model_Hr_Qc_Spec_Item *model = self.itemArray[indexPath.section];
    Model_Hr_Qc_Spec *specModel = self.array[indexPath.row];
    HNewQCSCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];
//    if (cell) {
//        
//        for (UIView *view in cell.contentView.subviews) {
//            
//            if (view) {
//                
//                [view removeFromSuperview];
//            }
//        }
//    }
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, 30, 30)];
    
    label.text = model.ITEM_NAME;
    [cell.contentView addSubview:label];
    cell.textField.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
    return cell;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

@end
